﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;

namespace 手机号码判断归属地
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string path = fbd.SelectedPath;
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "delete from T_Number";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "insert into T_Number(startNo,endNo,area) values(@sn,@en,@area)";
                    foreach (string file in files)
                    {
                        string 运营商 = Path.GetFileNameWithoutExtension(file);
                        string[] lines = File.ReadAllLines(file,Encoding.Default);
                        //不用StreamReader，因为文件很小，一次性加载也占不到很大内存，
                        //经反编译得知，File.ReadAllLines的默认编码为UTF-8
                        foreach (string line in lines)
                        {
                            string[] strs = line.Split('-');
                            string startNo = strs[0];
                            string endNo = strs[1];
                            string area = strs[2];
                            cmd.Parameters.Clear();
                            cmd.Parameters.Add(new SqlParameter("sn", startNo));
                            cmd.Parameters.Add(new SqlParameter("en", endNo));
                            cmd.Parameters.Add(new SqlParameter("area", 运营商+area));
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
            MessageBox.Show("插入数据成功！");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_Number where @num>=startNo and @num<=endNo";
                    cmd.Parameters.Add(new SqlParameter("num",txtPhone.Text));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            string result = reader.GetString(reader.GetOrdinal("area"));
                            MessageBox.Show("" + result);
                        }
                        else
                        {
                            MessageBox.Show("找不到运营商信息！");
                        }
                    }
                }
            }
        }
    }
}
